package com.example.agrigreennewssys.controller;


import com.example.agrigreennewssys.entity.ArticleLike;
import com.example.agrigreennewssys.entity.Comment;
import com.example.agrigreennewssys.entity.CommentLike;
import com.example.agrigreennewssys.service.CommentService;
import com.example.agrigreennewssys.utils.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 光的代言人
 */
@RestController
@RequestMapping("/api/comments")
public class CommentController {

    @Autowired
    private CommentService commentService;


    //所有评论
    @GetMapping("/list")
    public Map<String, Object> getComments(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "8") Integer limit) {
        PageHelper.startPage(page, limit);
        Map<String, Object> result = new HashMap<>();
        List<Comment> comments = commentService.getAllComments();
        PageInfo<Comment> pageInfo = new PageInfo<>(comments);
        result.put("code", 200);
        result.put("data", pageInfo.getList());
        result.put("msg", "查询成功");
        result.put("total", pageInfo.getTotal());
//            result.put("page", page);
        return result;
    }

    @PostMapping("/add")
    public Result addComment(@RequestBody Comment comment) {
        try {
            Comment savedComment = commentService.addComment(comment);
            return Result.success(savedComment);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(500, "添加失败!");
        }
    }

    @DeleteMapping("/{commentId}")
    public Result deleteComment(@PathVariable Long commentId, @RequestParam Long userId) {
        try {
            int rows = commentService.deleteComment(commentId, userId);
            return Result.success(rows);
        } catch (Exception e) {
            return Result.error(500, "删除失败!");
        }
    }

    // 更新评论状态
    @PutMapping("/status/{commentId}")
    public Result updateCommentStatus(@PathVariable Long commentId, @RequestParam Integer status) {
        try {
            boolean success = commentService.updateCommentStatus(commentId, status);
            return Result.success(success);
        } catch (Exception e) {
            return Result.error(500, "更新失败!");
        }
    }


    /**
     * 根据文章ID获取评论列表
     */
    @GetMapping("/article/{articleId}")
    public Result getCommentsByArticleId(@PathVariable Long articleId, @RequestParam(required = false) Long userId) {
        try {
            List<Comment> comments = commentService.getCommentsByArticleId(articleId, userId);
//            System.out.println(comments);
            return Result.success(comments);
        } catch (Exception e) {
            return Result.error(500, "查询失败!");
        }
    }


    @PostMapping("/like/{commentId}")
    public Result likeComment(@PathVariable Long commentId, @RequestParam Long userId, @RequestParam Long articleId) {
        System.out.println("文章：" + articleId);
        try {
            boolean success = commentService.likeComment(commentId, userId, articleId);
            return success ? Result.success("点赞成功") : Result.error(400, "已点赞过");
        } catch (Exception e) {
            return Result.error(500, "点赞失败：" + e.getMessage());
        }

    }

    @PostMapping("/unlike/{commentId}")
    public Result unlikeComment(@PathVariable Long commentId, @RequestParam Long userId) {
        try {
            boolean success = commentService.unlikeComment(commentId, userId);
            return success ? Result.success("取消点赞成功") : Result.error(400, "未点赞过");
        } catch (Exception e) {
            return Result.error(500, "取消点赞失败：" + e.getMessage());
        }
    }

    @GetMapping("/liked-status/{commentId}")
    public Result getCommentLikedStatus(@PathVariable Long commentId, @RequestParam Long userId) {
        boolean isLiked = commentService.isCommentLiked(commentId, userId);
        return Result.success(isLiked);
    }


    // 获取点赞列表（含关联信息）
    @PostMapping("/like/list")
    public Map<String, Object> getCommentLikeList(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit,@RequestBody CommentLike commentLike) {
        PageHelper.startPage(page, limit);
        System.out.println("xxx:"+commentLike);
        List<CommentLike> commentLikeList = commentService.selectCommentLikeList(commentLike.getNickName(), commentLike.getArticleTitle(), commentLike.getContent());
        PageInfo<CommentLike> pageInfo = new PageInfo<>(commentLikeList);
        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("data", pageInfo.getList());
        result.put("msg", "查询成功");
        result.put("total", pageInfo.getTotal());
        return result;
    }
}
